<?php

/*
 * litchicms
 * Copyright (C) ${date} Author: tiefa(539247@qq.com)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
!defined('BRK') && exit("!");

if (!function_exists('Component_ui_category')) {
    $BRK_GLOBAL['ComponentValidationFunction'][] = 'Component_Validation_category';
    $BRK_GLOBAL['ComponentEndingFunction'][] = 'Component_Ending_category';
    $BRK_GLOBAL['ComponentDeleteOneDateFunction'][] = 'Component_DeleteOneDate_category';

    function Component_ui_category($arr=array()) {
        static $cate;
        static $CATEGORY_ROOT;
        static $cids = array();
        static $relationships;
        if (!class_exists('category') || empty($cate)) {
            if (is_file($file = SYSTEM . 'cache/category/' . $GLOBALS['CATEGORY_ID'] . '.php')) {
                require $file;
            } else {
                require 'lr.class.php';
                $cate = new category($GLOBALS['CATEGORY_ID'], $GLOBALS['COLLECTION_ENAME']);
                $cate->loadsub();
                $CATEGORY_ROOT = cat2expert($cate);
                foreach ($CATEGORY_ROOT as $k => $v) {
                    $cids[] = $v['cid'];
                    $relationships[$v['cid']] = $k;
                }
                check_make_dir(SYSTEM . 'cache/category');
                $content = "<?php\n!defined('BRK') && exit(\"!\");\n";

                $content.="\$CATEGORY_ROOT=" . var2string($CATEGORY_ROOT) . ";";
                $content.="\$cids=" . var2string($cids) . ";";
                $content.="\$relationships=" . var2string($relationships) . ";";
                file_put_contents($file, $content);

                $coll_include_cat = "<?php\n!defined('BRK') && exit(\"!\");
require SYSTEM.'cache/category/" . $GLOBALS['CATEGORY_ID'] . ".php';";

                file_put_contents(SYSTEM . 'cache/category/coll_' . $GLOBALS['COLLECTION_ID'] . '.php', $coll_include_cat);
            }
        }
        if (array_key_exists('COLLECTION_ID', $arr)) {

            //

            return array('CATEGORY_ROOT' => $CATEGORY_ROOT, 'cids' => $cids, 'relationships' => $relationships);
        }
        if (array_key_exists('collection_value', $arr)) {
            if (!is_array($arr['collection_value'])) {
                $arr['collection_value'] = array(0);
            }
            $input = '';
            foreach ($CATEGORY_ROOT as $value) {
                $input.="<div>" . str_repeat("&nbsp;&nbsp;&nbsp;", $value['clayer']) . "<label><input type='checkbox' name='Component[category][]' " . (in_array($value['cid'], $arr['collection_value']) ? "checked" : "") . " value='" . $value['cid'] . "'/>" . replace_yy($value['cname']) . "</label></div>";
            }
            //        $input = input_text_control('title', $arr['collection_value'], 'title');
            return "
                    <div class='content_group_row'>
                        <table class='thetable' border='0' cellpadding='0' cellspacing='1'>
                            <tr>
                                <td class='td1' bgcolor='#ffffff'>
                                    <div>
                                        <div class='cname'><b>所属分类</b>:</div>
                                        <div class='cdesc'>可以多选哦...</div>
                                    </div>
                                </td>
                                <td class='td2' >
                                    <div>
                                        <div class='select_div'>
                                            <div class='m3'>
                                            $input
                                            </div>
                                        </div>
                                    </div>
                                </td>
                            </tr>
                        </table>
                    </div>
                    ";
        }
    }

}


if (!function_exists('Component_Validation_category')) {

    function Component_Validation_category() {
        //$_POST['Component']['category']
        global $FIELDS_hidden;

        if (empty($_POST['Component']['category']) || !is_array($_POST['Component']['category'])) {
            $_POST['Component']['category'] = array($FIELDS_hidden['category']['cids'][0]);
        } else {
            foreach ($_POST['Component']['category'] as $k => &$value) {
                $value = intval($value);
                if (!in_array($value, $FIELDS_hidden['category']['cids'])) {
                    unset($_POST['Component']['category'][$k]);
                }
            }
            unset($value);
        }
        return TRUE;
    }

}



if (!function_exists('Component_buildUiParam_category')) {

    function Component_buildUiParam_category($COLLECTION_ID=0, $ONE_DATA=NULL) {
        if (!$COLLECTION_ID || !$ONE_DATA) {
            return array('collection_value' => FALSE);
        }
        $rs = brk_select('ccc_relationships', '*', "WHERE collection_id=" . $COLLECTION_ID . " AND id=" . $ONE_DATA['id']);
        $incats = array();
        while ($row = $GLOBALS['Db']->fetch_array($rs)) {
            $incats[] = $row['cid'];
        }
        return array('collection_value' => $incats);
    }

}


if (!function_exists('Component_Ending_category')) {

    function Component_Ending_category() {
        brk_delete('ccc_relationships', "WHERE collection_id=" . $GLOBALS['COLLECTION_ID'] . " AND id=" . $GLOBALS['RECORD_ID']);
        foreach ($_POST['Component']['category'] as $value) {
            $dataarr = array(
                'collection_id' => $GLOBALS['COLLECTION_ID'],
                'id' => $GLOBALS['RECORD_ID'],
                'cid' => $value
            );
            brk_insert('ccc_relationships', $dataarr);
        }

        return TRUE;
    }

}

if (!function_exists('Component_buildField_category')) {

    function Component_buildField_category() {
        $field = array();
        foreach ($_POST['Component']['category'] as $value) {
            $dataarr = array(
                'collection_id' => $GLOBALS['COLLECTION_ID'],
                'id' => $GLOBALS['RECORD_ID'],
                'cid' => $value
            );
            $field[] = $value;
        }

        return array('category' => implode(',', $field), 'category_cache' => _brk_get_onedata_cat($field));
    }

}


/*
 * brk_get_onedata_cat
 */
if (!function_exists('_brk_get_onedata_cat')) {

    function _brk_get_onedata_cat($arr) {
        global $FIELDS_hidden;
        $temp = array();
        foreach ($arr as $value) {
            $temp[] = $FIELDS_hidden['category']['CATEGORY_ROOT'][$FIELDS_hidden['category']['relationships'][$value]];
        }
        return addslashes(serialize($temp));
    }

}


/**
 * list_select_category
 */
if (!function_exists('list_select_category')) {

    function list_select_category() {
        global $COLLECTION_TABLENAME, $COLLECTION_ID, $RT, $_PAGE, $Db;
//        $num = brk_select_join(
//                        $COLLECTION_TABLENAME . ",ccc_relationships",
//                        "count({0}.id)",
//                        "INNER JOIN {1} ON {0}.id={1}.id WHERE {0}.status=1 AND {1}.collection_id =" . $COLLECTION_ID . " AND  {1}.cid=" . intval($RT['v']['cid']));
//
//        $_PAGE['num'] = current($Db->fetch_array($num));
//        $_PAGE['pageCount'] = ceil($_PAGE['num'] / $_PAGE['oenPageNum']);
//        $_PAGE['current'] = $_PAGE['pageCount'] < $_PAGE['input'] ? $_PAGE['pageCount'] : $_PAGE['input'];
//        $_PAGE['current'] < 1 && $_PAGE['current'] = 1;
//
//        $rs = brk_select_join(
//                        $COLLECTION_TABLENAME . ",ccc_relationships",
//                        "{0}.*",
//                        "INNER JOIN {1} ON {0}.id={1}.id WHERE {0}.status=1 AND {1}.collection_id =" . $COLLECTION_ID . " AND  {1}.cid=" . intval($RT['v']['cid']) . " ORDER BY {0}.top DESC,{0}.sort DESC,{0}.id DESC LIMIT " . ($_PAGE['current'] - 1) * $_PAGE['oenPageNum'] . "," . $_PAGE['oenPageNum'] . " ");
//        return $rs;

        $array = array('partid' => $GLOBALS['COLLECTION_ID'], 'pageid' => 'a', 'num' => !empty($RT['v']['pagena']) ? $RT['v']['pagena'] : 10, 'cid' => intval($RT['v']['cid']), 'sub' => 0, 'isbg' => TRUE);
//                92:array('sort'=>'排序方式','num'=>条数,'channel'=>'目标频道')
//                $rs = brk_select($GLOBALS['COLLECTION_TABLENAME'], '*', "WHERE status=1 ORDER BY top DESC,sort DESC LIMIT " . ($_PAGE['current'] - 1) * $_PAGE['oenPageNum'] . "," . $_PAGE['oenPageNum'] . " ");
        return COLL_list($array);
//($data|COLL_list:92:array('fields'=>'id',category'=>分类id,'sub'=>是否包含子类,'sort'=>'排序方式','num'=>条数,'channel'=>'目标频道')
    }

}




/**
 * list_select_category
 */
if (!function_exists('Component_DeleteOneDate_category')) {

    function Component_DeleteOneDate_category($id) {
        global $COLLECTION_ID;
        brk_delete('ccc_relationships', "WHERE id=$id AND collection_id=$COLLECTION_ID");
    }

}
